using UnityEngine;
using System.Collections;

public class Defender : Action {
	
	public int hpCurrent = 10000;
	public int hpMax = 10000;
	
	public int damageOverTime = 0;
	
	public bool canRespawn = false;
	public float respawnDelay = 2;		// not yet implement
	public PathNode[] respownPathNodes;
	
	private float respawnDelayCounter = 0;
	
	// Use this for initialization
	void Start () {
		
		if(canRespawn && respownPathNodes.Length == 0)
		{
			print("GameObject: " + gameObject.name + "Can respawn but has no respownPathNodes");
			return;
		}
	}
	
	// Update is called once per frame
	void Update () {
	}
	void onDie()
	{
		if(canRespawn)	onRespawn();
		
	}
	void onRespawn()
	{
		transform.position = respownPathNodes[Random.Range(0,respownPathNodes.Length)].transform.position;
		hpCurrent = hpMax;
	}
	void onDestory()
	{
		GameObject.Destroy(gameObject);
	}
	
	
	public void AllDefender(Message msg)
	{
		print("Defender: " + gameObject.name + " receive " + msg.type.ToString());
		switch(msg.type)
		{
			case Message.MessageType.ATTACK:
			{
				processBeAttack(msg);

				break;
			}
		}
	}
	
	public void processBeAttack(Message msg) {
		if (((Attacker)(msg.array[1])).gameObject != gameObject) {
			Skill skill = (Skill)msg.array[0];
			bool isHitted = skill.atkDecision.checkTarget(this);
			if (isHitted)
			{
				skill.apply(this);
				msg.sendBackward(Message.HitSuccess(skill,this));
			}
		//	print("QAQ" + gameObject + "   " + msg.array[1]);
		}
	}
	
	public void takeDamage(int dam) {
		hpCurrent -= dam;
		if (hpCurrent < 0)	onDie();
		
	}
	
	public void Attack()
	{
		if(state.actionState != ActionState.BeHit)
		{
			
		}
	}
	
		
}
	